﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FCDB.Entity;
using FCDB.Entity.EntityEx;
using FCDB.Entity.Enum;


namespace FCDB.Repository
{
    public class WFWorkFlowRepository : LogicBase,ILogic
    {
        public IList<WfProcessType> GetWorkFlowType()
        {
            return DBHelper.WfProcessType.ToList();
        }
        public WfProcess GetProcess(Guid guid)
        {
            return DBHelper.WfProcess.FirstOrDefault(t => t.ProcessGUID == guid);
        }

        public IList<WfProcess> GetAllProcess(EntityStatus[] status = null)
        {
            var quere = from item in DBHelper.WfProcess
                        where item.Status == (short)EntityStatus.Normal
                        select item;

            if (null != status)
            {
                var statusList = status.Cast<short>().ToList();
                quere = quere.Where(w => statusList.Contains(w.Status.Value));
            }

            return quere.ToList();
        }

        public IList<WfProcess> GetTotalProcess(EntityStatus[] status = null)
        {
            var quere = from item in DBHelper.WfProcess
                        select item;

            if (null != status)
            {
                var statusList = status.Cast<short>().ToList();
                quere = quere.Where(w => statusList.Contains(w.Status.Value));
            }

            return quere.ToList();
        }

        public IList<WfProcessEx> GetProcessByTypeID(long WorkFlowTypeID=0)
        {
            var query = from item in DBHelper.WfProcess
                        join wfType in DBHelper.WfProcessType on item.WfProcessTypeID equals wfType.ID
                        where item.Status == 1
                        select new WfProcessEx()
                        {
                            CreatedDateTime = item.CreatedDateTime,
                            WfProcessTypeID = item.WfProcessTypeID,
                            ProcessTypeName = wfType.Name,
                            Description = item.Description,
                            LastUpdatedDateTime = item.LastUpdatedDateTime,
                            PageUrl = item.PageUrl,
                            ProcessGUID = item.ProcessGUID,
                            ProcessName = item.ProcessName,
                            Remark = item.Remark,
                            RowVersionID = item.RowVersionID,
                            Status = item.Status,
                            XmlFileName = item.XmlFileName,
                            XmlFilePath = item.XmlFilePath
                        };
            if (WorkFlowTypeID > 0)
            {
                query = query.Where(t => t.WfProcessTypeID == WorkFlowTypeID);
            }
           return  query.ToList();
             
        }

        public IList<MyWfProcess> GetMyProcess(Guid UserId)
        {
            var quere = from item in DBHelper.MyWfProcess
                        join wfProcess in DBHelper.WfProcess on item.ProcessGUID equals wfProcess.ProcessGUID
                        where item.UserID == UserId && wfProcess.Status == (short)EntityStatus.Normal
                        select item;
            return quere.ToList();
        }
        public IList<WfProcessEx> GetPagedList(int skip, int take, long WorkFlowTypeID,string keyword, out int total)
        {
            var query = from item in DBHelper.WfProcess
                        join wfType in DBHelper.WfProcessType on item.WfProcessTypeID equals wfType.ID
                        where item.Status == (short)EntityStatus.Normal
                        select new WfProcessEx()
                        {
                            CreatedDateTime = item.CreatedDateTime,
                            WfProcessTypeID = item.WfProcessTypeID,
                            ProcessTypeName = wfType.Name,
                            Description = item.Description,
                            LastUpdatedDateTime = item.LastUpdatedDateTime,
                            PageUrl = item.PageUrl,
                            ProcessGUID = item.ProcessGUID,
                            ProcessName = item.ProcessName,
                            Remark = item.Remark,
                            RowVersionID = item.RowVersionID,
                            Status = item.Status,
                            XmlFileName = item.XmlFileName,
                            XmlFilePath = item.XmlFilePath
                        };
            if (WorkFlowTypeID > 0)
            {
                query = query.Where(t => t.WfProcessTypeID == WorkFlowTypeID);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(t => t.ProcessName.IndexOf(keyword) > -1);
            }
            total = query.Count();
            IList<WfProcessEx> list = query.OrderByDescending(t => t.LastUpdatedDateTime).Skip(skip).Take(take).ToList();
            return list;
        }
        public List<WfProcessEx> GetMyPagedList(int skip, int take, long WorkFlowTypeID, Guid UserID, string keyword, out int total)
        {
            var Process = from item in DBHelper.MyWfProcess
                          join wfProcess in DBHelper.WfProcess on item.ProcessGUID equals wfProcess.ProcessGUID
                          where item.UserID == UserID && wfProcess.Status == (short)EntityStatus.Normal
                          select item;

            IList<MyWfProcess> myProcess = Process.ToList();
            List<WfProcessEx> myWfProcess = new List<WfProcessEx>();
            foreach (var i in myProcess)
            {
                var query = from item in DBHelper.WfProcess
                            join wfType in DBHelper.WfProcessType on item.WfProcessTypeID equals wfType.ID
                            where item.Status == (short)EntityStatus.Normal && item.ProcessGUID == i.ProcessGUID
                            select new WfProcessEx()
                            {
                                CreatedDateTime = item.CreatedDateTime,
                                WfProcessTypeID = item.WfProcessTypeID,
                                ProcessTypeName = wfType.Name,
                                Description = item.Description,
                                LastUpdatedDateTime = item.LastUpdatedDateTime,
                                PageUrl = item.PageUrl,
                                ProcessGUID = item.ProcessGUID,
                                ProcessName = item.ProcessName,
                                Remark = item.Remark,
                                RowVersionID = item.RowVersionID,
                                Status = item.Status,
                                XmlFileName = item.XmlFileName,
                                XmlFilePath = item.XmlFilePath
                            };

                if (WorkFlowTypeID > 0)
                {
                    query = query.Where(t => t.WfProcessTypeID == WorkFlowTypeID);
                }
                if (!string.IsNullOrEmpty(keyword))
                {
                    query = query.Where(t => t.ProcessName.IndexOf(keyword) > -1);
                }
                myWfProcess.AddRange(query.ToList());
                
            }
            total = myWfProcess.Count();
            myWfProcess = myWfProcess.OrderByDescending(t => t.LastUpdatedDateTime).Skip(skip).Take(take).ToList();
            return myWfProcess;
        }
    }
}
